package com.yaoxun.console.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yaoxun.console.exception.JsonException;

@Service
public class JsonService {

	@Autowired
	private ObjectMapper objectMapper;
	
	public String toJsonString(Object value) {
		try {
			return objectMapper.writeValueAsString(value);
		} catch (JsonProcessingException e) {
			throw new JsonException(e);
		}
	}
	
	public <T> T parse(String content, Class<T> valueType) {
		try {
			return objectMapper.readValue(content, valueType);
		} catch (JsonProcessingException e) {
			throw new JsonException(e);
		}
	}
	
}
